/**
 * Created by novem on 2015/10/21.
 */
window.onload=function(){
    var supplierId = $.getUrlParam("supplierId");
    var url = getUrl();
    var userId = getUserId();
    $("#url").attr('href','yiren_tijiaodingdan.html?supplierId='+supplierId);
    $.jsonp({
        url: 'http://'+url+'/api/supplier/supplier_detail-'+supplierId,
        data: {},
        callbackParameter: "callback",
        success: function (data, textStatus, xOptions) {
            var shopId = data.supplier.shopId;
            $("#chat").attr("onclick","easeMobGoChat("+data.supplier.telphone+")");
            var complied_tpl = juicer($("#info_tpl").text());
            var html= complied_tpl.render(data);
            $(".info_detail").append(html);
            var complied_tpl1 = juicer($("#info_tpl1").text());
            var html1= complied_tpl1.render(data);
            $("#shop_detail").append(html1)
            $(".w_shac").attr('data-shoucang',data.supplier.shopId);
            //$(".sc").attr('data-sc',shopsId);;
            $.jsonp({
                url: 'http://'+url+'/api/supplier/comment_list-'+supplierId,
                data: {},
                callbackParameter: "callback",
                success: function (data, textStatus, xOptions) {
                    $("#all_pj").attr('href','yiren_allpingjia.html?supplierId='+supplierId);
                    var len = data.arrays.length-1;
                    if(len<0){
                        var html = "";
                    }else{
                    //var unixTimestamp = new Date(data.arrays[0].createTime * 1000);
                    //data.arrays[0].createTime = unixTimestamp.toLocaleString();
                    var html = '<li class="ui-border-b"><div class="ui-comment-list-detail clearfix"><div class="ui-comment-list-user clearfix"><div class="ui-comment-list-avatar">'+
                        '<img src="'+data.arrays[0].photo+'" width="40" height="40" alt=""></div>'+
                        '<div class="ui-comment-list-info"><p>'+data.arrays[0].nickName+'</p><p class="ui-txt-muted">'+getTime(data.arrays[0].createTime)+'</p></div></div>'+
                        '<div class="ui-comment-list-star"><span class="ui-star ui-star-'+data.arrays[0].star+'"></span>'+data.arrays[0].star+'分</div></div>'+
                        '<div class="ui-comment-list-content">'+
                        ''+data.arrays[0].comment+'</div></li>';
                    }
                    $("#ui-comment-list").append(html);
                    $("#comment_tpl").text(data.totalCount+"人评价");
                },
                error: function (xOptions, textStatus) {}
            });
            $.jsonp({
                url: 'http://'+url+'/api/collect/collect_shops_list',
                data: {userId:userId},
                callbackParameter: "callback",
                success: function (data, textStatus, xOptions) {
                    $.each(data.arrays,function(index,ele){
                        var samewayId = ele.shopsId;
                        if(samewayId==shopId){
                            $(".sc").addClass("btn").text("已收藏").css({'font-size':'14px','color':'#fff'});
                        }
                    })
                },
                error: function (xOptions, textStatus) {}
            });
        },
        error: function (xOptions, textStatus) {
        }
    });
    //收藏
    $(document).on('click','.w_shac',function(){
        var shopsId = $(this).data('shoucang');
        //console.log(shopsId)
        $.jsonp({
            url: 'http://'+url+'/api/collect/collect_shops',
            data: {shopsId:shopsId,userId:userId},
            callbackParameter: "callback",
            success: function (data, textStatus, xOptions) {
                var code = data.code;
                if(code==1) {
                    $(".sc").removeClass("btn").text("收藏").css({'font-size':'14px','color':'#fff'});
                    showToast("取消收藏成功")
                }else{
                    $(".sc").addClass("btn").text("已收藏").css({'font-size':'14px','color':'#fff'});
                    showToast("收藏成功")
                }
            },
            error: function (xOptions, textStatus) {
            }
        });

    });
}
function getTime(t){
    var nt = t*1000;
    var n = new Date(nt);
    return formatDate(n);
}
function formatDate(now)   {
    var   year=now.getFullYear();
    var   month=now.getMonth()+1;
    var   date=now.getDate();
    return   year+"-"+(month>=10?month:"0"+month)+"-"+(date>=10?date:"0"+date);
}